package com.hawthorn.redis.service.role.impl;

import com.hawthorn.component.constant.RedisConstant;
import com.hawthorn.component.utils.common.StrMyUtil;
import com.hawthorn.entity.dto.SysRoleMenuDTO;
import com.hawthorn.platform.redis.RedisMyClient;
import com.hawthorn.redis.repository.RoleMenuCacheMapper;
import com.hawthorn.redis.service.role.RoleMenuCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>todo 加载角色菜单关系缓存服务实现类 </p>
 * @author: andyten@tom.com | xukaixing@hotmail.com
 * @created: 2021/11/26 12:32 PM
 * @version v1.0.1
 * @copyright: Copyright (c)  andyten
 */
@Service
@Slf4j
public class RoleMenuCacheServiceImpl implements RoleMenuCacheService
{
  @Resource
  RedisMyClient redisMyClient;

  @Resource
  private RoleMenuCacheMapper roleMenuCacheMapper;

  @Transactional(readOnly = true)
  public void init()
  {

    List<SysRoleMenuDTO> roleMenuCache = roleMenuCacheMapper.selectRoleMenu();
    String key;
    // todo 先删除redis中的key
    redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_ROLEMENU_PREFIX, "*"));

    // String roleMenus = "";
    // for (SysRoleMenuDTO sysRoleMenuDTO : roleMenuCache)
    // {
    //   key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_ROLEMENU_PREFIX, sysRoleMenuDTO.getRoleId());
    //   String tempValue = redisMyClient.get(key);
    //   if ("".equals(tempValue))
    //   {
    //     roleMenus = sysRoleMenuDTO.getMenuId();
    //   } else
    //   {
    //     roleMenus += ":" + sysRoleMenuDTO.getMenuId();
    //   }
    //   redisMyClient.set(key, roleMenus);
    // }
    log.info("====== load rolemenu data to redis success ======");
  }
}
